package com.ashun.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import com.ashun.config.auth.DemoAuthenticationProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenwenshun on 16/9/30.
 * 权限控制
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DemoAuthenticationProvider demoAuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 每一个用于只允许登陆一个示例，异地登陆挤掉当前登陆
        http.sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(false);
//        http.authorizeRequests().anyRequest().authenticated();
        http.authorizeRequests().antMatchers("/css/**","/h2-console/**","/resources/**","/distributorApi/**").permitAll()
                .anyRequest().authenticated();
        http.formLogin().loginPage("/login").permitAll().and().rememberMe();
//        http.logout().logoutUrl("/logout").logoutSuccessUrl("/").invalidateHttpSession(true);
        http.logout().permitAll();
        http.csrf().disable();
        http.headers().frameOptions().sameOrigin();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(demoAuthenticationProvider);
    }

    //    @Autowired
//    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//        auth
//                .inMemoryAuthentication()
//                .withUser("user").password("password").roles("USER");
//
//
//    }


}
